﻿using System;
using System.Text;
using System.Windows.Forms;
using NoNameGoServer.Client.Gui.Properties;

namespace NoNameGoServer.Client.Gui {
    public partial class ConnectDialog : Form {
        public ConnectDialog() {
            InitializeComponent();
            _port.Text = Settings.Default.Port.ToString();
            _userName.Text = Settings.Default.UserName;
            _password.Text = Settings.Default.Password;
            if (!string.IsNullOrEmpty(Settings.Default.HostNames)) {
                string[] hostNames = Settings.Default.HostNames.Split(';');
                foreach (string hostName in hostNames) {
                    _hostName.Items.Add(hostName);
                }
            }
            _hostName.Text = Settings.Default.HostName;
        }

        private void _cancel_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void _ok_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.OK;
            Settings.Default.Port = ushort.Parse(_port.Text);
            Settings.Default.UserName = _userName.Text;
            Settings.Default.Password = _password.Text;
            Settings.Default.HostName = _hostName.Text;
            StringBuilder hostNames = new StringBuilder();
            foreach (var item in _hostName.Items) {
                hostNames.Append(item);
                hostNames.Append(";");
            }
            Settings.Default.HostNames = hostNames.ToString().Trim(';');
            Settings.Default.Save();
            Close();
        }

        public string HostName {
            get { return _hostName.Text; }
        }

        public int Port {
            get { return int.Parse(_port.Text); }
        }

        public string UserName {
            get { return _userName.Text; }
        }

        public string Password {
            get { return _password.Text; }
        }
    }
}
